import java.util.Arrays;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/1/9
 * Time:11:12
 **/
public class Main {
    //public static void main(String[] args) {
        //数组拷贝

        //for

        /*int[] array1 = {1,2,3,4,5,6};
        int[] array2 = new int[array1.length];
        for (int i = 0; i < array1.length; i++) {
            array2[i] = array1[i];
        }
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));*/

        //Arrays.copyOf

        /*int[] array1 = {1,2,3,4,5,6};
        int[] array2 = Arrays.copyOf(array1,array1.length);
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));*/

        //System.arraycopy()

        /*int[] array1 = {1,2,3,4,5,6};
        int[] array2 = new int[array1.length];
        System.out.println(Arrays.toString(array1));
        System.arraycopy(array1,0,array2,0,array1.length);
        System.out.println(Arrays.toString(array2));*/

        //array1.clone()

        /*int[] array1 = {1,2,3,4,5,6};
        int[] array2 = array1.clone();
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));*/

        //Arrays.copyOfRange()

        /*int[] array1 = {1,2,3,4,5,6};
        int[] array2 = Arrays.copyOfRange(array1,2,5);
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));*/
    //}


    //1.实现一个方法 avg, 以数组为参数, 求数组中所有元素的平均值(注意方法的返回值类型).
    public static double avg(int[] a) {
        int s = 0;
        for (int x : a) {
            s += x;
        }
        return s*1.0/a.length;
    }
    public static void main1(String[] args) {
        int[] arr={1,2,3,4,5,6};
        double ret=avg(arr);
        System.out.println(ret);
    }

    //2实现一个方法 sum, 以数组为参数, 求数组所有元素之和.
    public static int sum(int[] a) {
        int s = 0;
        for (int x : a) {
            s += x;
        }
        return s;
    }
    public static void main2(String[] args) {
        int[] arr={1,2,3,4,5,6};
        int ret=sum(arr);
        System.out.println(ret);
    }

    //3实现一个方法transform,以数组为参数,循环将数组中的每个元素乘以2,并设置到对应的数组元素上.
    //              例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
    public static void transform (int[] arr2) {
        for(int x:arr2){
            System.out.print(x*2+" ");
        }
    }

    public static void main3(String[] args) {
        int[] arr={1,2,3,4,5,6};
        transform(arr);
    }
    //4实现一个方法 printArray, 以数组为参数, 循环访问数组中的每个元素, 打印每个元素的值.
    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
    public static void main4(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        printArray(arr);
    }

    //5创建一个 int 类型的数组, 元素个数为 100, 并把每个元素依次设置为 1 - 100
    public static void main5(String[] args) {
        int[] arr = new int[100];
        for (int i = 0; i < 100 ; i++) {
            arr[i] = i+1;
        }
        System.out.println(Arrays.toString(arr));
    }

    //6给定一个整型数组, 实现冒泡排序(升序排序)
    public static void bubbleSort(int[] a) {
        for (int i = 0; i < a.length-1; i++) {
            for (int j = 0; j < a.length-1; j++) {
                if(a[j]>a[j+1]){
                    int tmp = a[j+1];
                    a[j+1] = a[j];
                    a[j] = tmp;
                }
            }
        }
    }
    public static void main6(String[] args) {
        int[] arr = {2,4,3,6,9,7,1,8};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    //7给定一个整型数组, 判定数组是否有序（递增）
    public static boolean isUp(int[] a){
        for (int i = 0; i < a.length-1; i++) {
            if(a[i+1]<a[i]){
                return false;
            }
        }
        return true;
    }

    public static void main7(String[] args) {
        int[] a = {1,2,3,4,5,6,7,8};
        int[] b = {3,2,5,7,4,8,6,0};
        System.out.println(isUp(a));
        System.out.println(isUp(b));
    }
    //8给定一个有序整型数组, 实现二分查找
    public static int halfSearch(int[] a,int key){
        int low = 0;
        int high = a.length-1;
        int mid = 0;
        while(low <= high){
            mid = (low + high)/2;
            if(a[mid]==key)
                return mid;
            else if(a[mid] > key) {
                high = mid -1;
            }else{
                low = mid+1;
            }
        }
        return -1;
    }

    public static void main8(String[] args) {
        int[] a = {1,2,3,4,5,6,7,8};
        System.out.println(halfSearch(a,6));
    }

    //9实现一个方法 copyOf, 对一个整型数组进行拷贝, 得到一个新的数组.
    public static void copyOf(int[] a) {
        int[] a2 = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            a2[i] = a[i];
        }
        System.out.println(Arrays.toString(a2));
    }

    public static void main9(String[] args) {
        int[] a1 = {1,2,3,4,5,6};
        copyOf(a1);
    }

    //10实现一个方法 toString, 把一个整型数组转换成字符串.
    //       例如数组 {1, 2, 3} , 返回的字符串为 "[1, 2, 3]", 注意 逗号 的位置和数量.
    //先初始化一个字符串变量为" [ "，遍历数组，str += 数组的每一项，并且除最后一项外都加上" , "，最后 str += " ] "。
    public static String toString(int[] a){
        String str = "[";
        for (int i = 0; i < a.length; i++) {
            str += a[i];
            if (i < a.length-1){
                str += ",";
            }
        }
        str += "]";
        return str;
    }
    public static void main10(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        String arr2 = toString(arr);
        System.out.println(arr2);
    }

    //给定整型数组, 把所有的偶数放到数组前面, 把所有奇数放到数组后面.

    public static void func(int[] array){
        int i = 0;
        int j = array.length-1;
        while(i<j) {
            while(i<j && array[i]%2 == 0){//偶数
                i++;
            }
            while (i<j && array[j]%2 != 0){//奇数
                j--;
            }
            int tmp = array[j];
            array[j] = array[i];
            array[i] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8};
        func(array);
        System.out.println(Arrays.toString(array));
    }

    //给定两个整型数组, 交换两个数组的内容

    public static void change(int[] arr1,int[] arr2){
        int len1 = arr1.length;
        int len2 = arr2.length;
        int len = len1>len2?len2:len1;//取短的
        for (int i = 0; i < len; i++) {
            int tmp = arr1[i];
            arr1[i]=arr2[i];
            arr2[i]=tmp;
        }
    }

    public static void main12(String[] args) {
        int[] arr1 = {1,2,3,4,5,6,7,8};
        int[] arr2 = {6,5,4,3,2,1};
        change(arr1,arr2);
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
    }
}
